package net.lcheng.sellerCenter.controller;

import net.lcheng.model.User;
import net.lcheng.model.WxShop;
import net.lcheng.model.WxShopBanner;
import net.lcheng.model.WxShopCategory;
import net.lcheng.service.UserService;
import net.lcheng.service.WxShopCategoryService;
import net.lcheng.service.WxShopService;
import net.lcheng.vo.WxShopCategoryModel;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by zwr on 2016/7/5 0005.
 */
@Controller
public class WxShopCategoryController {
    @Autowired
    private WxShopCategoryService wxShopCategoryService;
    @Autowired
    private UserService userService;
    @Autowired
    private WxShopService wxShopService;
    /*显示分类**/
    @RequestMapping(value = "shop_category", method = RequestMethod.GET)
    public String shopCategoryGet(Model model) {
        Subject currentUser = SecurityUtils.getSubject();
        if (currentUser.isAuthenticated()) {
            String userPhoneNum = currentUser.getSession().getAttribute("UserPhoneNum").toString();
            //获取用户信息
            User user = userService.getUserByPhone(userPhoneNum);
            //获取店铺id
            WxShop shop = wxShopService.getShopInforByUserId(user.getUserId());
            List<WxShopCategory> wxShopCategoriesList = wxShopCategoryService.getWxShopCategoryByShopId(shop.getShopId());
            model.addAttribute("wxShopCategoriesList", wxShopCategoriesList);
        }
        return "shop_category";
    }
    /**
     * 添加分类get
     */
    @RequestMapping(value = "add/shop_category", method = RequestMethod.GET)
    public String addShopCategoryGet(Model model) {
        model.addAttribute("WxShopCategoryModel", new WxShopCategoryModel());
        return "add_category";
    }

    /**
     * 添加分类Post
     */
    @RequestMapping(value = "add/shop_category", method = RequestMethod.POST)
    public String addShopCategoryPost(Model model, @Valid @ModelAttribute("WxShopCategoryModel") WxShopCategoryModel wxShopCategoryModel, BindingResult result) {
        if (result.hasErrors()) {
            return "add_category";
        }
        Subject currentUser = SecurityUtils.getSubject();
        String userPhoneNum = currentUser.getSession().getAttribute("UserPhoneNum").toString();
        //获取用户信息
        User user = userService.getUserByPhone(userPhoneNum);
        //获取店铺id
        WxShop shop = wxShopService.getShopInforByUserId(user.getUserId());
        WxShopCategory wxShopCategory = new WxShopCategory();
        wxShopCategory.setCategoryId(UUID.randomUUID().toString());
        wxShopCategory.setCategoryName(wxShopCategoryModel.getCategoryName());
        wxShopCategory.setOrderNum(wxShopCategoryModel.getOrderNum());
        wxShopCategory.setShopId(shop.getShopId());
        wxShopCategory.setCreateOn(new Date());
        wxShopCategory.setStatus(1);
        int returnResult = wxShopCategoryService.addCategory(wxShopCategory);
        if (returnResult > 0) {
            return "redirect:/shop_category";
        } else
            return "add_category";
    }

    /**
     * 修改分类get
     */
    @RequestMapping(value = "modify/shop_category", method = RequestMethod.GET)
    public String modifyShopCategoryGet(Model model, @RequestParam String id) {
        WxShopCategory wxShopCategory = wxShopCategoryService.getWxShopCategoryByCategoryId(id);
//        把wxShopCategory值传递到wxShopCategoryModel里面去
        WxShopCategoryModel wxShopCategoryModel = new WxShopCategoryModel();
        wxShopCategoryModel.setCategoryId(wxShopCategory.getCategoryId());
        wxShopCategoryModel.setOrderNum(wxShopCategory.getOrderNum());
        wxShopCategoryModel.setCategoryName(wxShopCategory.getCategoryName());
        wxShopCategoryModel.setProductCount(wxShopCategory.getProductCount());
        model.addAttribute("wxShopCategory", wxShopCategory);
        model.addAttribute("WxShopCategoryModel", wxShopCategoryModel);
        return "modify_category";
    }

    /**
     * 修改分类Post
     */
    @RequestMapping(value = "modify/shop_category", method = RequestMethod.POST)
    public String modifyShopCategoryPost(Model model, @Valid @ModelAttribute("WxShopCategoryModel") WxShopCategoryModel wxShopCategoryModel, BindingResult result) {
        if (result.hasErrors()) {
            return "modify_category";
        }
        WxShopCategory wxShopCategory = new WxShopCategory();
        wxShopCategory.setCategoryId(wxShopCategoryModel.getCategoryId());
        wxShopCategory.setCategoryName(wxShopCategoryModel.getCategoryName());
        wxShopCategory.setOrderNum(wxShopCategoryModel.getOrderNum());
        wxShopCategory.setModifyOn(new Date());
        int returnResult = wxShopCategoryService.modifyCategory(wxShopCategory);
        if (returnResult > 0) {
            return "redirect:/shop_category";
        } else
            return "modify_category";
    }
    /**
     * 删除分类(更新状态)
     */
    @RequestMapping(value = "delete/shop_category", method = RequestMethod.GET)
    public String deleteShopCategoryGet(Model model, @RequestParam String id) {
        WxShopCategory wxShopCategory = new WxShopCategory();
        wxShopCategory.setCategoryId(id);
        wxShopCategory.setStatus(2);
        wxShopCategoryService.modifyStatus(wxShopCategory);
        return "redirect:/shop_category";
    }

}
